<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2013-08-19 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'mobiapi/modules/base.mod.php';
require ROOT_PATH."service/tag.service.php";
require ROOT_PATH."service/story.service.php";

class ModuleObj extends BaseModule
{
	private $service;
	private $storyService;
	
	function __construct($request)
	{
		parent::__construct($request);
		
		$this->service = new TagService();
		$this->storyService = new StoryService();
		
		$this->excute();
	}
	
	function excute()
	{
		switch($this->act)
		{
			case 'hot':
				$this->getHotTag();
				break;
			case 'info':
				$this->getStoryList();
				break;
			case 'search':
				$this->searchTag();
				break;
			case 'storyList':
				$this->getStoryList();
				break;
			default:
				$this->message('ACTION_NOT_EXISTS', STATUS_E);
		}
		
		$this->showResponse();
	}
	
	function getHotTag()
	{
		$result = $this->service->hot($this->request);
		
		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);
		
		$this->response['size'] = (string) count($result);
		$this->response['list'] = $result;
	}
	
	function getTagInfo()
	{
		$this->checkRequest('info');
		
		$result = $this->service->get($this->request['id']);
		
		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);
		
		$this->response['info'] = $result;
	}
	
	function searchTag()
	{
		$this->checkRequest('name_search');
		
		$this->request['tag'] = $this->request['name'];
		$result = $this->service->search($this->request);
		
		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);
		
		$this->response['size'] = (string) count($result);
		$this->response['list'] = $result;
	}
	
	function getStoryList()
	{
		$this->checkRequest('info');

		$this->request['tag'] = $this->request['id'];
		$this->request['uid'] = $this->user['id'];
		
		if (!check_array($this->service->get($this->request['id']))) $this->message('NOT_EXISTS', STATUS_E);
		
		$result = $this->storyService->get_list_by_tag($this->request);
		
		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);
		
		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}
}